package com.study.demo.data;

import android.text.TextUtils;

import com.study.demo.Constants;
import com.study.demo.api.forum.ForumApi;
import com.study.demo.bean.ThreadListData;
import com.study.demo.db.CustomThread;
import com.study.demo.greendao.CustomThreadDao;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;


/**
 * Created by sll on 2016/6/3.
 */
public class ThreadRepository {


    private CustomThreadDao mThreadDao;
    private ForumApi mForumApi;

    @Inject
    public ThreadRepository(CustomThreadDao mThreadDao, ForumApi mForumApi) {
        this.mThreadDao = mThreadDao;
        this.mForumApi = mForumApi;
    }

    public Observable<List<CustomThread>> getThreadListObservable(final int type,
                                                                  PublishSubject<List<CustomThread>> mSubject) {
        Observable<List<CustomThread>> firstObservable = Observable.fromCallable(
                () -> mThreadDao.queryBuilder().where(CustomThreadDao.Properties.Type.eq(type)).list()
        );
        return firstObservable.concatWith(mSubject)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<ThreadListData> getRecommendThreadList(final String lastTid, String lastTamp,
                                                             final PublishSubject<List<CustomThread>> mSubject) {
        return mForumApi.getRecommendThreadList(lastTid, lastTamp)
                .doOnNext(threadListData -> {
                    if (threadListData != null && threadListData.result != null) {
                        cacheThreadList(0, TextUtils.isEmpty(lastTid), threadListData.result.data);
                    }
                    if (mSubject != null) {
                        mSubject.onNext(mThreadDao.queryBuilder()
                                .where(CustomThreadDao.Properties.Type.eq(Constants.TYPE_RECOMMEND))
                                .list());
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<ThreadListData> getThreadsList(final String fid, final String lastTid,
                                                     String lastTamp, String type, final PublishSubject<List<CustomThread>> mSubject) {
        return mForumApi.getThreadsList(fid, lastTid, lastTamp, type)
                .doOnNext(threadListData -> {
                            if (threadListData != null && threadListData.result != null) {
                                cacheThreadList(Integer.valueOf(fid), TextUtils.isEmpty(lastTid),
                                        threadListData.result.data);
                            }
                            if (mSubject != null) {
                                mSubject.onNext(mThreadDao.queryBuilder()
                                        .where(CustomThreadDao.Properties.Type.eq(Integer.valueOf(fid)))
                                        .list());
                            }
                        }
                ).observeOn(AndroidSchedulers.mainThread());
    }

    private void cacheThreadList(int type, boolean clear, List<CustomThread> customThreads) {
        if (clear) {
            mThreadDao.queryBuilder()
                    .where(CustomThreadDao.Properties.Type.eq(type))
                    .buildDelete()
                    .executeDeleteWithoutDetachingEntities();
        }
        for (CustomThread customThread : customThreads) {
            if (mThreadDao.queryBuilder()
                    .where(CustomThreadDao.Properties.Tid.eq(customThread.getTid()), CustomThreadDao.Properties.Type.eq(type))
                    .count() == 0) {
                customThread.setType(type);
                mThreadDao.insert(customThread);
            }
        }
    }
}
